package scau.gsmg.misp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSON;
import net.sf.json.JSONArray;

import org.springframework.stereotype.Service;

import scau.gsmg.misp.dao.IMenuDao;
import scau.gsmg.misp.service.IMenuService;
import scau.gsmg.misp.vo.GoodsType;
import scau.gsmg.misp.vo.Permission;

@Service
public class MenuService implements IMenuService {

	private IMenuDao menuDao;
 // map对象模拟json键值对
	@Resource
	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	
	@Override
	public List showMenu(int loginID) {

		List<Permission> permissions = menuDao.searchMenu(loginID);
		List list = new ArrayList();
		Map<String, Object> map = null;
		for (Permission p : permissions) {

			if (p.getSort() == null||"".equals(p.getSort())) {
				System.out.println(p.getId()+"tea tea teatea");
				map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("text", p.getName());
				if("1".equals(p.getState())){
					System.out.println(p.getId()+"coffee tea milk");
					List kk=new ArrayList();
					kk=nodeMenu(p,loginID);
					map.put("children", kk);
				}else{
					if( p.getUrl()!=null){
						Map<String, Object> mapUrl = new HashMap<String, Object>();
						mapUrl.put("url",  p.getUrl());
						map.put("attributes",mapUrl);
					}
					
				}
				list.add(map);
				System.out.println("Lenght"+list.size());
			}
			
		}
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i)+"zhuzzhuzhzu");
		}
		return list;
	}

	private List nodeMenu(Permission per,int loginID) {

		List<Permission> permissions = menuDao.searchMenu(loginID);
		List listNode = new ArrayList();
		Map<String, Object> map = null;
			for (Permission p : permissions) {
		 		if (p.getSort()!=null&&p.getSort().equals(per.getId()+"")) {
		 			System.out.println("a beautiful fu"+p.getId());
					map = new HashMap<String, Object>();
					map.put("id", p.getId());
					map.put("text", p.getName());
					if("1".equals(p.getState())){
						System.out.println(p.getId()+"*****");
						List ll=nodeMenu(p,loginID);
						map.put("children", ll);
						System.out.println(p.getId()+"llllll"+ll.size());
						
					}else{
						if( p.getUrl()!=null){
							Map<String, Object> mapUrl = new HashMap<String, Object>();
							mapUrl.put("url",  p.getUrl());
							map.put("attributes",mapUrl);
						}
						
					}
					listNode.add(map);
					System.out.println("listNode.add(map);"+listNode.size());
					
				}
		 		
				
			}
			
		return listNode;

	}

	

}
